package com.szqhyxj.youxiangju.biz.core;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;

import java.io.File;

public class PhotoBridge {

    /**
     * 拍照，传一个文件过来
     *
     * @param activity  界面
     * @param code      请求 code
     * @param photoFile 存储照片的文件
     */
    public static void startToTakePicture(Activity activity, int code, File photoFile) {
        Uri uri = FileProvider.getUriForFile(activity, "com.szqhyxj.youxiangju.provider", photoFile);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        if (intent.resolveActivity(activity.getPackageManager()) != null) {
            activity.startActivityForResult(intent, code);
        }
//        Uri uri = Uri.fromFile(photoFile);
//        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//        if (intent.resolveActivity(activity.getPackageManager()) != null) {
//            activity.startActivityForResult(intent, code);
//        }
    }

    /**
     * 相册选择
     *
     * @param activity 界面
     * @param code     请求 code
     */
    public static void startToGallery(Activity activity, int code) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setType("image/*");
        activity.startActivityForResult(intent, code);
    }
}